package com.graduate.cec.service;


import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.URLUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.graduate.cec.common.JsonPage;
import com.graduate.cec.common.ResponseCode;
import com.graduate.cec.exception.CecServiceException;
import com.graduate.cec.mapper.AuthenticationMapper;
import com.graduate.cec.mapper.UserMapper;
import com.graduate.cec.mapper.UserRoleMapper;
import com.graduate.cec.pojo.dto.AuthenticationSimpleDTO;
import com.graduate.cec.pojo.dto.FeedbackSimpleDTO;
import com.graduate.cec.pojo.entity.*;
import com.graduate.cec.pojo.vo.AuthenticationVO;
import com.graduate.cec.pojo.vo.FeedbackVO;
import com.graduate.cec.util.CopyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class AuthenticationService {
    @Autowired
    AuthenticationMapper authenticationMapper;

    @Value("${kbs.jwt.secret-key}")
    private String secretKey;
    @Resource
    private UserMapper userMapper;
    @Resource
    private UserService userService;
    @Resource
    private UserRoleMapper userRoleMapper;
    public void insertFeedback(Authentication authentication){
        log.info("返回的feedback:{}",authentication);
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String phone = (String) principal;
        Long id = userMapper.selectIdByPhone(phone);
        authentication.setUserId(id);

        int i = authenticationMapper.insertSelective(authentication);

        if (i != 1){
            throw new CecServiceException(ResponseCode.INSERT_INFO_ERROR,"新增失败!");
        }

    }


    public void updateByKey(Authentication authentication){
        userService.updateRole(authentication.getUserId());
        int i = authenticationMapper.deleteByPrimaryKey(authentication.getId());
        if (i == 1){
            return;
        }else{
            throw new CecServiceException(ResponseCode.UPDATE_INFO_ERROR,"修改失败");
        }
    }

    public JsonPage<AuthenticationVO> getListByExample(AuthenticationSimpleDTO authenticationSimpleDTO){

        PageHelper.startPage(authenticationSimpleDTO.getPageNum(), authenticationSimpleDTO.getPageSize());

        AuthenticationExample authenticationExample = new AuthenticationExample();

        List<Authentication> authentications = authenticationMapper.selectByExample(authenticationExample);

        List<AuthenticationVO> authenticationVOS = CopyUtil.copyList(authentications, AuthenticationVO.class);
        for (Authentication authentication : authentications) {
            User user = userMapper.selectNameById(authentication.getUserId());
            for (AuthenticationVO authenticationVO : authenticationVOS) {
                if(authenticationVO.getId() == authentication.getId()){
                    authenticationVO.setUserName(user.getNickname());
                }

            }
        }

        return JsonPage.restPage(new PageInfo<>(authenticationVOS));
    }




}
